//
//  ALMapController.m
//  Ummy.G
//
//  Created by 安林 on 16/10/3.
//  Copyright © 2016年 安林. All rights reserved.
//

#import "ALMapController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ALMapController ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)backLocation:(id)sender;

- (IBAction)back:(id)sender;

@property(nonatomic,strong)CLLocationManager *manger;

@end

@implementation ALMapController



- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    //反地理编码
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //地理标志
        CLPlacemark *mark=[placemarks firstObject];
        if (mark.locality) {
            userLocation.title=mark.locality;
            userLocation.subtitle=mark.name;
        }
    }];

}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _manger.desiredAccuracy = kCLLocationAccuracyBest;
    
    //更新距离(每十米产生一次事件)
    _manger.distanceFilter = 10;
    _manger.delegate = self;
    
    _manger = [[CLLocationManager alloc]init];
    [_manger requestAlwaysAuthorization];
    self.mapView.delegate = self;
    
    //允许跟踪定位
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //开始更新位置，产生更新事件
    [_manger startUpdatingLocation];
    
    self.mapView.showsUserLocation = YES;
    
    
}


- (IBAction)backLocation:(id)sender {
    
    CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(38.0412, 114.4708);
    
    MKCoordinateSpan span = MKCoordinateSpanMake(0.02, 0.02);
    
    //显示区域
    MKCoordinateRegion region = MKCoordinateRegionMake(cord, span);
    [self.mapView setRegion:region animated:YES];
}

- (IBAction)back:(id)sender {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
